﻿using IntelligentAgriculture.Domain.Entity;
using IntelligentAgriculture.Domain.Interface;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentAgriculture.Domain.Impl
{
    public class UnitRepository: IUnitRepository
    {

        /// <summary>
        /// 数据库上下文
        /// </summary>

        private readonly AgricultureDbContext context;
        public UnitRepository(AgricultureDbContext context)
        {
            this.context = context;
        }


        /// <summary>
        /// 省列表
        /// </summary>
        /// <returns></returns>
        public  async Task<List<Province>> ProvinceList()
        {
            return context.Provinces.ToList();
        }

        /// <summary>
        /// 市列表
        /// </summary>
        /// <param name="provinceId"></param>
        /// <returns></returns>
        public async Task<List<City>> CityList(int provinceId)
        {
            return context.Cities.Where(c => c.ProVinceId == provinceId).ToList();
        }


        /// <summary>
        /// 区列表
        /// </summary>
        /// <param name="cityId"></param>
        /// <returns></returns>
        public async Task<List<Area>> UnitList(int cityId)
        {
            return context.Areas.Where(a => a.CityId == cityId).ToList();
        }


        /// <summary>
        /// 单位添加
        /// </summary>
        /// <param name="unit"></param>
        /// <returns></returns>
        public async Task<int> UnitAdd(Unit unit)
        {
            context.Units.Add(unit);
            return await context.SaveChangesAsync();
        }


        /// <summary>
        /// 获取单位所有
        /// </summary>
        /// <returns></returns>
        public async Task<IQueryable<Unit>> Unit()
        {
            return context.Units;
        }


        /// <summary>
        /// 单位列表信息
        /// </summary>
        /// <param name="companyName"></param>
        /// <returns></returns>
        public async  Task<List<Unit>> GetUnitList(string companyName)
        {

            var sheeps = context.Units.Where(c => c.Isdelete == 0).AsQueryable().AsNoTracking();
            if (!string.IsNullOrEmpty(companyName))
            {
                sheeps = sheeps.Where(c => c.CompanyName.Contains(companyName));
            }          
            return sheeps.ToList();
        }







    }




}
